package com.memorypalace.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	public static Date getTodayBegin(Date date){
		Date result = new Date();
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		result = date;
		return result;
	}
	/**
	 * 
	 * @param date
	 * @return 23:59:59
	 */
	public static Date getTodayEnd(Date date){
		Date result = new Date();
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		
		result = new Date(date.getTime()+(24*3600-1)*1000);
		return result;
	}
	/**
	 * 多少天之前的0:0:0
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getTodayBegin(Date date,Integer days){
		Date result = new Date();
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		result = new Date(DateUtil.getTodayBegin(date).getTime()-days*24*3600*1000);
		return result;
	}
	
	public static Date string2Date(String dateStr){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	public static String date2String(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = null;
		dateStr = sdf.format(date);
		return dateStr;
	}
}
